package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.Visitors;

import cn.wolfcode.wolf2w.repository.VisitorsRepository;
import cn.wolfcode.wolf2w.service.IVisitorsService;
import cn.wolfcode.wolf2w.util.VisitorVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;



import java.util.List;


@Service
public class VisitorsServiceImpl implements IVisitorsService {


    @Autowired
    private VisitorsRepository visitorsRepository;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Override
    public void save(Visitors visitors) {
        visitors.setId(null);
        visitorsRepository.save(visitors);
    }

    @Override
    public void update(Visitors visitors) {
        visitorsRepository.save(visitors);
    }

    @Override
    public void delete(String id) {
        visitorsRepository.deleteById(id);
    }

    @Override
    public Visitors get(String id) {
        return visitorsRepository.findById(id).orElse(null);
    }

    @Override
    public List<Visitors> list() {
        return visitorsRepository.findAll();
    }

    @Override
    public List<Visitors> findByUserIdIn(List<String> visitorId) {
       return null;
    }

    @Override
    public Visitors findByOwnerId(String id) {
        return  visitorsRepository.findByOwnerId(id);

    }

    @Override
    public void updateVisitorVo(VisitorVo vo) {
        Query query = new Query();
        query.addCriteria(Criteria.where("ownerId").is(vo.getOwnerId()));
        Update update = new Update();
        update.set("totalView",vo.getTotalView());
        update.set("visitorIds",vo.getVisitorIds());
         mongoTemplate.updateMulti(query,update,Visitors.class);
    }

}
